import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '@/store'
import Login from '@/views/login'
import Layout from '@/views/layout'
import Dashboard from '@/views/dashboard'
import OrganizationManage from '@/views/organization-manage'
import InstitutionsJobsarea from '@/views/institutions-jobs-area'
import FreightManage from '@/views/freight-manage'
import Carmodels from '@/views/car-models'
import Vehicle from '@/views/vehicle'
import Carregister from '@/views/car-register'
import Operationalrange from '@/views/operational-range'
import WorkArrangemanage from '@/views/workArrange-manage'
import Ordermanage from '@/views/order-manage'
import Waybill from '@/views/waybill'
import Transporttask from '@/views/transport-task'
import Linemanage from '@/views/line-manage'
import PickUptask from '@/views/pickUptask'
import DispatchTask from '@/views/dispatchTask'
import Driver from '@/views/driver'
import EditOrder from "@/views/transit/editorder.vue";

Vue.use(VueRouter)
const router = new VueRouter({
   routes : [
    { path: "/login", component: Login },
    {
      path: "/",
      component: Layout,
      children: [
        { path: "/dashboard", component: Dashboard },
        { path: "/branches/organization-manage", component: OrganizationManage },
        { path: "/branches/institutions-jobs-area", component: InstitutionsJobsarea },
        { path: "/transit/freight-manage", component: FreightManage },
        { path: "/transit/car-models", component: Carmodels },
        { path: "/transit/vehicle", component: Vehicle },
        { path: "/transit/car-register", component: Carregister },
        { path: "/branches/operational-range", component: Operationalrange },
        { path: "/transit/driver", component: Driver },
        { path: "/transit/workArrange-manage", component: WorkArrangemanage },
        { path: "/business/order-manage", component: Ordermanage },
        { path: "/business/waybill", component: Waybill },
        { path: "/transport/transport-task", component: Transporttask },
        { path: "/transport/line-manage", component: Linemanage },
        { path: "/branches/pickUptask", component: PickUptask },
        { path: "/branches/dispatchTask", component: DispatchTask },
        {
          path: "transit/car-register-info/:id",
          component: () => import("@/views/re/receiptReview"),
        },
        {
          path: "/transit/driver-detail",
          component: () => import("@/views/transit/driver-detail"),
        },
        {
          path: "/transit/workArrange-setting",
          component: () => import("@/views/transit/workArrange-setting"),
        },
        {
          path:'/transit/editorder',
          component: EditOrder
        },
        {
          path: "/transit/waybillDetail",
          component: () => import("@/views/transit/waybillDetail"),
        },
        {
          path: "/transit/task",
          component: () => import("@/views/transit/talk"),
        },
      ],
    },
],
});
const whiteList = ['/login']
router.beforeEach((to, from, next) => {
  const token = store.state.user.token;
  if (to.path !== "/login" && !token) return next("/login");
  if (token) {
    if (to.path === '/login') {
      next('/')
    } else {
      next()
    }
  } else {
    if (whiteList.includes(to.path)) {
      next()
    } else {
      next('/login')
    }
  }
});

export default router
